
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.io.File" %>
<%@ page import="cn.school.mybatis.pojo.Student" %>
<%@ page import="cn.school.mybatis.service.StudentService" %>
<%--
  Created by IntelliJ IDEA.
  User: zhaoyang
  Date: 2020/8/4
  Time: 9:44 上午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>增加学生</title>
</head>
<body>
<%

//    request.setCharacterEncoding("utf-8");
//    String sname=request.getParameter("stuName");
//    String sex=request.getParameter("stuSex");
//    String telephone=request.getParameter("stuPhone");
//    String qq=request.getParameter("stuQQ");
//    String wechat=request.getParameter("stuWechat");
//    int gradeid = new Integer(request.getParameter("stuGrade"));
//    String enterdate=request.getParameter("enterTime");
//    String fromcity=request.getParameter("fromCity");
//    String birthday=request.getParameter("stuBirth");
//    String address=request.getParameter("stuAddr");
//    String pass="123456";
//    String idcardpic="";

    String sname="";
    String sex="";
    String telephone="";
    String qq="";
    String wechat="";
    int gradeid =0;
    String enterdate="";
    String fromcity="";
    String birthday="";
    String address="";
    String pass="123456";
    String idcardpic="";

    //文件上传
    FileItemFactory item=new DiskFileItemFactory();
    ServletFileUpload upload=new ServletFileUpload(item);
    upload.setHeaderEncoding("utf-8");
    List<FileItem> list=upload.parseRequest(request);
   for(FileItem fi:list){
        if(fi.isFormField()){
            //普通的表单元素
//            out.print("<h2>"+fi.getFieldName()+":"+fi.getString("utf-8")+"</h2>");
            switch (fi.getFieldName()){
                case "stuName":
                    sname=fi.getString("utf-8");
                    break;
                case "stuSex":
                    sex=fi.getString("utf-8");
                    break;
                case "stuPhone":
                    telephone=fi.getString("utf-8");
                    break;
                case "stuQQ":
                    qq=fi.getString("utf-8");
                    break;
                case "stuWechat":
                    wechat=fi.getString("utf-8");
                    break;
                case "stuGrade":
                    gradeid=Integer.parseInt(fi.getString("utf-8"));
                    break;
                case "enterTime":
                    enterdate=fi.getString("utf-8");
                    break;
                case "stuBirth":
                    birthday=fi.getString("utf-8");
                    break;
                case "fromCity":
                    fromcity=fi.getString("utf-8");
                    break;
                case "stuAddr":
                    address=fi.getString("utf-8");
                    break;

            }

        }else{
            //文件
            if (fi.getSize()==0){
//                out.print("<h2>没有文件！</h2>");
                continue;
            }
//            out.print("<h2>这是一个文件，文件名："+fi.getName()+"，元素名："+fi.getFieldName()+"</h2>");
            String path=application.getRealPath("images");
            //文件要换一个名字,保证后缀是相同
            String fName =fi.getName();
            String houZhui=fName.substring(fName.lastIndexOf("."));
            String fileName= UUID.randomUUID()+houZhui;
            File f=new File(path,fileName);
//            out.print(f.getAbsolutePath());
            idcardpic=fileName;
            fi.write(f);
        }
    }


    Student student=new Student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address,pass,idcardpic);
//    out.print(student);
    int result = 0;
    try{
        result = StudentService.STUDENT_SERVICE.add(student);
    }catch (Exception e){}

    if (result==1){
        out.println("<script>alert('"+result+"条信息增加成功！');location.replace('"+request.getContextPath()+"/AdminLTEdemo01/s_student/s_studentInfo.jsp')</script>");

    }else {
        out.println("<script>alert('信息增加失败...');history.back(-1);</script>");

    }



    //文件上传
//
//    FileItemFactory item=new DiskFileItemFactory();
//    ServletFileUpload upload=new ServletFileUpload(item);
//    upload.setHeaderEncoding("utf-8");
//    List<FileItem> list=upload.parseRequest(request);
//   for(FileItem fi:list){
//        if(fi.isFormField()){
//            //普通的表单元素
//            out.print("<h2>"+fi.getFieldName()+":"+fi.getString("utf-8")+"</h2>");
//        }else{
//            //文件
//            out.print("get file>>>>>>>>>>>>.");
//            if (fi.getSize()==0){
//                out.print("<h2>没有文件！</h2>");
//                continue;
//            }
//            out.print("<h2>这是一个文件，文件名："+fi.getName()+"，元素名："+fi.getFieldName()+"</h2>");
//            String path=application.getRealPath("images");
//            //文件要换一个名字,保证后缀是相同
//            String fName =fi.getName();
//            String houZhui=fName.substring(fName.lastIndexOf("."));
//            String fileName= UUID.randomUUID()+houZhui;
//            File f=new File(path,fileName);
//            out.print(f.getAbsolutePath());
//            fi.write(f);
//        }
//    }



%>

</body>
</html>
